package com.gamutsoft.amb.home;

import javax.servlet.http.HttpSession;

import com.gamutsoft.amb.base.action.BaseAction;
import com.gamutsoft.amb.common.constants.Constants;
import com.gamutsoft.amb.common.util.CookieUtil;
import com.gamutsoft.amb.user.model.SystemUser;
import com.gamutsoft.amb.user.service.ISystemUserService;



/**
 * @author Administrator
 *
 */
public class HomeAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private ISystemUserService systemUserService;
	/**
	 * 初始化主页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String init() throws Exception {
		//重session 中读取用户
		Object obj=this.getRequest().getSession().getAttribute(Constants.SESSION_USER);
		if(obj==null){
			//cookie读取
			SystemUser user=CookieUtil.searchCookiesUser(this.getRequest());
			if(user!=null && user.getPassword()!=null && user.getEmail()!=null){
				SystemUser bean=systemUserService.loginByUser(user);
				if(bean!=null){
					//保存session
					this.getRequest().getSession().setAttribute(Constants.SESSION_USER, bean);
				}
			}
		}
		return "toIndex";
	}
	
	/**
	 * 跳转到错误页面
	 */
	public String toError(){
		return "toError";
	}
	/**
	 * 登出，清session
	 * @return
	 */
	public String logout(){
		HttpSession session = super.getRequest().getSession();
		String[] valueNames=session.getValueNames();
		for(int i=0;i<valueNames.length;i++){
			session.removeAttribute(valueNames[i]);
		}
		//清除cookie
		CookieUtil.deleteCookiesUser(this.getRequest(), this.getResponse());
		return "toIndex";
	}
	 /**
	 * 404页面
	 * @return
	 */
    public String notFound(){
		return "not_found";
	}

	public void setSystemUserService(ISystemUserService systemUserService) {
		this.systemUserService = systemUserService;
	}

	
}
